package com.spark.core;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.ser.CustomSerializerFactory;

import com.spark.util.ReferenceType;
import com.spark.util.ReferenceSerializer;

public class JacksonMapper {

	private static ObjectMapper objectMapper;

	private JacksonMapper() {}

	public static ObjectMapper getInstance() {
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
			CustomSerializerFactory sf = new CustomSerializerFactory();
			sf.addGenericMapping(ReferenceType.class, new ReferenceSerializer());
			objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
			DateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			objectMapper.getSerializationConfig().setDateFormat(myDateFormat);
	        objectMapper.getDeserializationConfig().setDateFormat(myDateFormat); 
			objectMapper.setSerializerFactory(sf);
		}
		return objectMapper;
	}
}
